#include "StdAfx.h"
#include "FileUtil.h"
#include "md5.h"
#include <fstream>

#define BLOCK_SIZE 10240

std::string FileUtil::getfilename(const std::string & fullpath)
{
	std::string filename;

	char   drive[_MAX_DRIVE]; 
	char   dir[_MAX_DIR]; 
	char   fname[_MAX_FNAME]; 
	char   ext[_MAX_EXT]; 

	_splitpath(fullpath.c_str(), drive, dir, fname, ext );
	filename = fname;
	filename = filename + ext;
	return filename;
}

std::string FileUtil::getfilepath(const std::string & fullname)
{
	std::string filepath;

	char   drive[_MAX_DRIVE]; 
	char   dir[_MAX_DIR]; 
	char   fname[_MAX_FNAME]; 
	char   ext[_MAX_EXT]; 

	_splitpath(fullname.c_str(), drive, dir, fname, ext );
	filepath = drive;
	filepath = filepath + dir;
	return filepath;
}

std::string FileUtil::getfileMD5(const std::string &fullname)
{
	gloox::MD5 encoder;
	std::fstream file(fullname.c_str(), std::ios_base::in | std::ios_base::binary);
	char input[BLOCK_SIZE];
	while ( !file.eof() ) 
	{
		file.read(input, BLOCK_SIZE);
		int size = file.gcount();
		encoder.feed((const unsigned char*)input, size);
		if(size < BLOCK_SIZE)
			break;
	}
	encoder.finalize();
	return encoder.hex();
}
